﻿using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(USTimelineBase))]
public class USTimelineBaseEditor : Editor
{
    public GUIStyle defaultFloatingBackground = GUIStyle.none;
    public GUIStyle defaultTimelineBackground = GUIStyle.none;
    private Transform draggingObject;
    private bool hasDragged;
    protected Rect myArea = new Rect();
    private object targetTimeline;

    public virtual void AddDataToRightClickContextMenu(GenericMenu context, Transform transform)
    {
        ContextData userData = new ContextData();
        userData.mouseOver = transform;
        userData.currentSelection = Selection.activeTransform;
        bool flag = USSequencerUtility.IsTimelineContainer(transform);
        bool flag2 = USSequencerUtility.IsTimeline(transform);
        bool flag3 = USSequencerUtility.IsEvent(transform);
        bool flag4 = USSequencerUtility.HasPropertyTimeline(transform);
        if (USSequencerUtility.HasObserverTimeline(transform))
        {
            context.AddDisabledItem(new GUIContent("添加时间线/属性"));
            context.AddDisabledItem(new GUIContent("添加时间线/事件"));
            context.AddDisabledItem(new GUIContent("设置受影响对象"));
            context.AddDisabledItem(new GUIContent("删除"));
        }
        else
        {
            if (userData.currentSelection != null)
            {
                context.AddItem(new GUIContent("设置受影响对象为 : " + userData.currentSelection.name), false, new GenericMenu.MenuFunction2(USTimelineBaseEditor.ContextClickSetObject), userData);
            }
            else
            {
                context.AddDisabledItem(new GUIContent("设置受影响对象"));
            }
            context.AddItem(new GUIContent("添加时间线/事件"), false, new GenericMenu.MenuFunction2(USTimelineBaseEditor.ContextClickAddEventTimeline), userData);
            if (!flag4)
            {
                context.AddItem(new GUIContent("添加时间线/属性"), false, new GenericMenu.MenuFunction2(USTimelineBaseEditor.ContextClickAddPropertyTimeline), userData);
            }
            else
            {
                context.AddDisabledItem(new GUIContent("Add Timeline/Property"));
            }
            if ((flag2 || flag3) || flag)
            {
                context.AddItem(new GUIContent("删除"), false, new GenericMenu.MenuFunction2(USTimelineBaseEditor.ContextClickRemove), userData);
            }
            else
            {
                context.AddDisabledItem(new GUIContent("删除"));
            }
        }
    }

    public virtual void AddRightClickOptions(GenericMenu contextMenu, Vector2 mousePosition)
    {
    }

    private static void ContextClickAddEventTimeline(object obj)
    {
        if (!(obj is ContextData))
        {
            Debug.LogWarning("传入的数据不是ContextData类型.");
        }
        else
        {
            ContextData data = obj as ContextData;
            USTimelineContainer component = data.mouseOver.GetComponent<USTimelineContainer>();
            USTimelineBase base2 = data.mouseOver.GetComponent<USTimelineBase>();
            if (base2 != null)
            {
                component = base2.TimelineContainer;
            }
            if (component == null)
            {
                Debug.LogWarning("对象缺少组件USTimelineContainer");
            }
            else
            {
                string path = "EmptyPrefab";
                GameObject original = Resources.Load(path, typeof(GameObject)) as GameObject;
                if (original == null)
                {
                    Debug.Log("找不到预制组件EmptyPrefab，请检查资源文件夹");
                }
                else
                {
                    GameObject objectToUndo = UnityEngine.Object.Instantiate(original) as GameObject;
                    objectToUndo.transform.parent = component.transform;
                    objectToUndo.name = "事件时间线";
                    objectToUndo.AddComponent<USTimelineEvent>();
                    Undo.RegisterCreatedObjectUndo(objectToUndo, "添加新时间线");
                    EditorTimelineData.Data[component.transform].expanded = true;
                }
            }
        }
    }

    private static void ContextClickAddPropertyTimeline(object obj)
    {
        if (!(obj is ContextData))
        {
            Debug.LogWarning("传入的数据不是ContextData类型.");
        }
        else
        {
            ContextData data = obj as ContextData;
            USTimelineContainer component = data.mouseOver.GetComponent<USTimelineContainer>();
            USTimelineBase base2 = data.mouseOver.GetComponent<USTimelineBase>();
            if (base2 != null)
            {
                component = base2.TimelineContainer;
            }
            if (component == null)
            {
                Debug.LogWarning("对象缺少组件USTimelineContainer");
            }
            else
            {
                string path = "EmptyPrefab";
                GameObject original = Resources.Load(path, typeof(GameObject)) as GameObject;
                if (original == null)
                {
                    Debug.Log("找不到预制组件EmptyPrefab，请检查资源文件夹");
                }
                else
                {
                    GameObject objectToUndo = UnityEngine.Object.Instantiate(original) as GameObject;
                    objectToUndo.transform.parent = component.transform;
                    objectToUndo.name = "属性时间线";
                    objectToUndo.AddComponent<USTimelineProperty>();
                    Undo.RegisterCreatedObjectUndo(objectToUndo, "添加新时间线");
                    EditorTimelineData.Data[component.transform].expanded = true;
                }
            }
        }
    }

    private static void ContextClickRemove(object obj)
    {
        if (!(obj is ContextData))
        {
            Debug.LogWarning("传入的数据不是ContextData类型.");
        }
        else
        {
            ContextData data = obj as ContextData;
            if ((data.mouseOver != null) && (data.mouseOver.gameObject != null))
            {
                USTimelineBase component = data.mouseOver.gameObject.GetComponent<USTimelineBase>();
                if (component != null)
                {
                    component.Reset();
                }
                Undo.RegisterSceneUndo("删除 : " + data.mouseOver.name);
                UnityEngine.Object.DestroyImmediate(data.mouseOver.gameObject);
            }
        }
    }

    private static void ContextClickSetObject(object obj)
    {
        if (!(obj is ContextData))
        {
            Debug.LogWarning("传入的数据不是ContextData类型.");
        }
        else
        {
            ContextData data = obj as ContextData;
            USTimelineContainer component = data.mouseOver.GetComponent<USTimelineContainer>();
            USTimelineBase base2 = data.mouseOver.GetComponent<USTimelineBase>();
            if (base2 != null)
            {
                component = base2.TimelineContainer;
            }
            if (component == null)
            {
                Debug.LogWarning("对象缺少组件USTimelineContainer");
            }
            else
            {
                Undo.RegisterSceneUndo("设置受影响对象为 : " + data.currentSelection.name);
                component.AffectedObject = data.currentSelection;
            }
        }
    }

    public virtual void GetSelectableObjectUnderPos(ref List<UnityEngine.Object> selectedObjects, Rect selectionArea)
    {
    }

    public virtual void HandleFloatingInteraction(Transform interactionObject, Rect thisArea)
    {
        Event current = Event.current;
        if ((current.type == EventType.MouseDown) && (current.button == 0))
        {
            this.HandleFloatingLeftClick(interactionObject, current, thisArea);
        }
        if ((current.type == EventType.MouseUp) && (current.button == 0))
        {
            this.HandleFloatingLeftClickUp(interactionObject, current, thisArea);
        }
        if ((current.type == EventType.MouseDrag) && (current.button == 0))
        {
            this.HandleFloatingLeftClickDrag(interactionObject, current, thisArea);
        }
        if ((current.type == EventType.MouseDown) && (current.button == 1))
        {
            this.HandleFloatingRightClick(interactionObject, current, thisArea);
        }
    }

    public virtual void HandleFloatingLeftClick(Transform transform, Event evt, Rect thisArea)
    {
        this.hasDragged = false;
        this.draggingObject = transform;
        evt.Use();
    }

    public virtual void HandleFloatingLeftClickDrag(Transform transform, Event evt, Rect thisArea)
    {
        if (this.draggingObject != null)
        {
            this.hasDragged = true;
            bool flag = USSequencerUtility.IsTimelineContainer(this.draggingObject);
            bool flag2 = USSequencerUtility.IsTimelineContainer(transform);
            bool flag3 = USSequencerUtility.HasObserverTimeline(transform) || USSequencerUtility.HasObserverTimeline(this.draggingObject);
            if ((flag && flag2) && !flag3)
            {
                USTimelineContainer component = transform.GetComponent<USTimelineContainer>();
                USTimelineContainer container2 = this.draggingObject.GetComponent<USTimelineContainer>();
                int index = component.Index;
                component.Index = container2.Index;
                container2.Index = index;
                evt.Use();
            }
        }
    }

    public virtual void HandleFloatingLeftClickUp(Transform transform, Event evt, Rect thisArea)
    {
        this.draggingObject = null;
        if (this.hasDragged)
        {
            this.hasDragged = false;
        }
        else
        {
            bool flag = USSequencerUtility.IsTimelineContainer(transform);
            if (USSequencerUtility.IsTimeline(transform))
            {
                EditorGUIUtility.PingObject(transform);
                Selection.activeObject = transform;
            }
            if (flag)
            {
                EditorGUIUtility.PingObject(transform.GetComponent<USTimelineContainer>().AffectedObject);
                Selection.activeObject = transform.GetComponent<USTimelineContainer>().AffectedObject;
            }
        }
    }

    public virtual void HandleFloatingRightClick(Transform transform, Event evt, Rect thisArea)
    {
        GenericMenu context = new GenericMenu();
        this.AddDataToRightClickContextMenu(context, transform);
        context.ShowAsContext();
        evt.Use();
    }

    public virtual void OnDrag()
    {
    }

    public virtual void OnDragEnd()
    {
    }

    public virtual void OnDragStart()
    {
    }

    public virtual Rect OnFloatingBackground(Rect thisArea)
    {
        GUI.Box(thisArea, string.Empty, this.defaultFloatingBackground);
        GUI.Label(thisArea, "请实现OnTimelineRender");
        return thisArea;
    }

    public virtual void OnFloatingContents(Rect thisArea)
    {
        thisArea.height = this.myArea.height;
        GUI.Box(thisArea, string.Empty, this.defaultFloatingBackground);
        GUI.Label(thisArea, "请实现OnFloatingContents");
    }

    public virtual void OnTimelineBackground(Rect thisArea)
    {
        thisArea.height = this.myArea.height;
        GUI.Box(thisArea, string.Empty, this.defaultTimelineBackground);
        GUI.Label(thisArea, "请实现OnTimelineBackground");
    }

    public virtual void OnTimelineContents(Rect thisArea)
    {
        thisArea.height = this.myArea.height;
        GUI.Label(thisArea, "请实现OnTimelineContents");
    }

    public virtual void ProcessRemoveObject()
    {
    }

    public void Setup()
    {
        this.myArea.height = 17f;
        if (this.defaultFloatingBackground == GUIStyle.none)
        {
            GUISkin uSeqSkin = USEditorUtility.USeqSkin;
            if (uSeqSkin != null)
            {
                this.defaultFloatingBackground = uSeqSkin.GetStyle("TimelineLeftPaneBackground");
            }
        }
        if (this.defaultTimelineBackground == GUIStyle.none)
        {
            GUISkin skin2 = USEditorUtility.USeqSkin;
            if (skin2 != null)
            {
                this.defaultTimelineBackground = skin2.GetStyle("TimelinePaneBackground");
            }
        }
    }

    public Rect MyArea
    {
        get
        {
            return this.myArea;
        }
    }

    public object TargetTimeline
    {
        get
        {
            return this.targetTimeline;
        }
        set
        {
            this.targetTimeline = value;
        }
    }

    private class ContextData
    {
        public Transform currentSelection;
        public Transform mouseOver;
        public float time;
        public System.Type typeToCreate;
    }
}

